package components.contorls.superClass
{
	import components.core.UIComponent;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.events.FlexEvent;
	
	[Event(name="selectionChange", type="mx.events.FlexEvent")]
	public class BaseTabBar extends UIComponent
	{
		private var _dataProvider:Array = [];
		
		private var _selectedItem:BaseButton = null;
		
		private var _selectedIndex:int = -1;
		
		private var _childList:Array = [];
		
		private var _oldDataProvider:Array;
		
		private var _dataProviderChange:Boolean;
		
		private var _selectedIndexChange:Boolean;
		
		public function BaseTabBar()
		{
			super();
		}
		
		/***************************************
		 *
		 * 显示对象声明
		 *
		 * **************************************/
		
		
		/***************************************
		 *
		 * 验证
		 *
		 * **************************************/
		
		protected override function commitProperties():void
		{
			super.commitProperties();
			
			var i:int;
			var element:BaseButton;
			
			if(_dataProviderChange)
			{
				for (i = 0; i < _childList.length; i++)
				{
					element = _oldDataProvider[i] as BaseButton;
					element.removeEventListener(MouseEvent.CLICK, element_clickHandler);
				}
				
				_oldDataProvider=[];
				_childList = [];
				
				if (_selectedIndex + 1 > dataProvider.length)
					selectedIndex = -1;
				
				for (i = 0; i < _dataProvider.length; i++)
				{
					var elementName:String = _dataProvider[i];
					
					element = parent.getChildByName(elementName) as BaseButton;
					
					if (element)
					{
						element.addEventListener(MouseEvent.CLICK, element_clickHandler, false, 0, true);
						element.focusComp = focusComp;
						_childList.push(element);
						
						if (i == selectedIndex)
							selectedItem = element;
					}
				}
				
				_dataProviderChange=false;
			}
			
			if(_selectedIndexChange)
			{
				if (_selectedIndex == -1)
					selectedItem = null;
				else
				{
					element= _childList[selectedIndex] as BaseButton;
					selectedItem = element;
				}
				
				_selectedIndexChange=false;
			}
		}
		
		protected override function measureSizes():Boolean
		{
			return super.measureSizes();
		}
		
		
		protected override function updateDisplayList():void
		{
			super.updateDisplayList();
		}
		
		/***************************************
		 *
		 * 组件定义
		 *
		 * **************************************/
		
		
		[Inspectable]
		public function get dataProvider():Array
		{
			return _dataProvider;
		}
		
		public function set dataProvider(value:Array):void
		{
			_oldDataProvider=_dataProvider;
			
			_dataProvider = value;
			
			_dataProviderChange=true;
			
			invalidate();
		}
		
		[Inspectable(defaultValue="-1")]
		public function get selectedIndex():int
		{
			return _selectedIndex;
		}
		
		public function set selectedIndex(value:int):void
		{
			if(_selectedIndex==value)
				return ;
			
			_selectedIndex = value;
			
			_selectedIndexChange=true;
			invalidate();
		}
		
		/***************************************
		 *
		 * 其他代码
		 *
		 * **************************************/
		protected function element_clickHandler(event:Event):void
		{
			for (var i:int = 0; i < _childList.length; i++)
			{
				var element:DisplayObject = _childList[i] as DisplayObject;
				if (element == event.currentTarget)
				{
					selectedIndex = i;
					break;
				}
			}
		}
		
		public function get selectedItem():BaseButton
		{
			return _selectedItem;
		}
		
		public function set selectedItem(value:BaseButton):void
		{
			if(_selectedItem==value)
				return ;
			
			if (_selectedItem)
			{
				_selectedItem.selected = false;
				_selectedItem = null;
			}
			
			_selectedItem = value;
			if (_selectedItem)
				_selectedItem.selected = true;
			
			dispatchEvent(new FlexEvent(FlexEvent.SELECTION_CHANGE));
		}
		
		public override function destroy():void
		{
			super.destroy();
			_dataProvider=null;
			_selectedItem=null;
			_childList=null;
		}
		
		protected override function frameConstructedHandler(event:Event):void
		{
			super.frameConstructedHandler(event);
			
			visible=false;
		}
	}
}